package cn.gzmli.dtt.common.dao;

import cn.gzmli.dtt.common.entity.Codeinfo;
import cn.gzmli.dtt.common.entity.PageInfo;
import cn.gzmli.dtt.common.utils.BaseDao;
import cn.gzmli.dtt.common.utils.BaseUtil;
import com.xiaoleilu.hutool.date.DateUtil;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: yangxiong
 * @Date: 2018/7/13 16:15
 * @Description:
 */
@Repository
public class CodeInfoDao extends BaseDao<Codeinfo> {

    public int save(Codeinfo c) {
        return getJdbcTemplate().update("insert into " +
                        "codeinfo (codeInfoName, codeInfoType, codeMarkName, codeMarkType, orderNo, codeInfoStatus, description, createTime) " +
                        "VALUES (?,?,?,?,?,?,?,?)",
                c.getCodeInfoName(), c.getCodeInfoType(), c.getCodeMarkName(),c.getCodeMarkType(), 0, c.getCodeInfoStatus(),
                c.getDescription(), c.getCreateTime());
    }

    public int updateBean(Codeinfo c) {
        return getJdbcTemplate().update("update codeinfo set codeInfoName=?,codeInfoType=?,codeInfoStatus=?," +
                        "description=?,codeMarkName=?,codeMarkType=?  where codeInfoId=?",
                c.getCodeInfoName(), c.getCodeInfoType(), c.getCodeInfoStatus(), c.getDescription(), c.getCodeMarkName(),c.getCodeMarkType(), c.getCodeInfoId());
    }
    public int delete(Integer id){
        return getJdbcTemplate().update("delete from codeinfo where codeInfoId=?",id);
    }

    public List list(Codeinfo c, PageInfo pageInfo) throws Exception {
        String sql = "select * from codeinfo where 1=1";
        Map<String, Object> map = new HashMap<>();
        if (!StringUtils.isEmpty(c.getCodeInfoName())){
            map.put(" and codeInfoName LIKE ?", "%" + c.getCodeInfoName() + "%");
        }
        if (!StringUtils.isEmpty(c.getCodeInfoType())){
            map.put(" and codeInfoType = ?", c.getCodeInfoType());
        }
        if (!StringUtils.isEmpty(c.getCodeInfoStatus())){
            map.put(" and codeInfoStatus = ?", c.getCodeInfoStatus());
        }
        if (!StringUtils.isEmpty(c.getCreateTime())){
            map.put(" and DATE_FORMAT(createTime,'%Y-%m-%d') = ?", DateUtil.formatDate(c.getCreateTime()));
        }
        return BaseUtil.getData(new StringBuilder(sql), map, pageInfo, this);
    }
}
